﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using DataObjectsPhone;
using System.Collections.ObjectModel;
using Microsoft.Maps.MapControl;

namespace ee_wp_tourmaker
{
    public partial class ChallengePivot : PhoneApplicationPage
    {
        private Challenge content;

        public ChallengePivot()
        {
            InitializeComponent();
            App.MainModel.ChallengePivot = this;
            if (App.MainModel.SelectedChallenge != null)
                setChallenge();
        }

        public void setChallenge()
        {

            content = App.MainModel.SelectedChallenge;
            NameLabel.Text = content.Name;
            DescriptionLabel.Text = content.Description;
            CheckPointsCountLabel.Text = "Checkpoints: " + content.Checkpoints;
            PointsLabel.Text = "Points: " + content.checkpointsList.Count * 2;
            fillTableWithCheckpoints(checkpointsListBox, content.checkpointsList);
            

            if (!(App.ChallengeData.ActiveChallenges.Contains(content) || App.ChallengeData.DoneChallenges.Contains(content)))
            {
                AcceptButton.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                AcceptButton.Visibility = System.Windows.Visibility.Collapsed;
            }
        }
        private void fillTableWithCheckpoints(ListBox table, ObservableCollection<CheckPoint> objects)
        {
            if (objects != null)
            {
                for (int i = 0; i < objects.Count; i++)
                {
                    CheckPointCell cell = new CheckPointCell();
                    cell.setCheckPoint(objects[i]);
                    table.Items.Add(cell);
                }
            }
        }


        private void AcceptButton_Click(object sender, RoutedEventArgs e)
        {
            App.ChallengeData.ActiveChallenges.Add(content);
            App.ChallengeData.WebChallenges.Remove(content);
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }

        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/ChallengeCheckPointsMap.xaml", UriKind.Relative));
        }

        private void PivotContainer_Loaded(object sender, RoutedEventArgs e)
        {
            switch (((Pivot)sender).SelectedIndex)
            {
                case 0:
                    ApplicationBar.IsVisible = false;
                    break;

                case 1:
                    ApplicationBar.IsVisible = true;
                    break;

            }
        }
    }
}
